/*
 
Enterprise Social Network (ESN) is an open-source Java EE framework for
a social network application.

Copyright (C) 2006-07 (see CONTRIBUTORS file in project root)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

package esn.core.ejb.interceptor;

import javax.interceptor.*;

import org.jboss.logging.*;

/**
 * <p>EJB interceptor for application server specific logging.</p>
 * 
 * @author Anirban Basu
 *
 */
public class EJBExceptionLogger {
	//use JBossAS specific logging
	private static final Logger jbossLogger = Logger.getLogger(EJBExceptionLogger.class);
	
	/**
	 * Logs invocation time in nano seconds and exceptions, if thrown.
	 * 
	 * @param invocation
	 * @return
	 * @throws Exception exception thrown by the invocation is
	 * re-thrown for the caller(s) in chain to handle
	 */
	@AroundInvoke
	public Object log(InvocationContext invocation) throws Exception {
		long startTime = System.nanoTime();
		try {
			return invocation.proceed();
		}
		catch (Exception e) {
			jbossLogger.error("Method: " + invocation.getMethod()
					+ " threw exception",e);
			throw e; //re-throw for callers to handle in a chain
		}
		finally {
			long timeTaken = System.nanoTime() - startTime;
			jbossLogger.info("Method: " + invocation.getMethod()
					+ " invocation completed in " + timeTaken + " ns");
		}
	}
}
